package com.evola.dbsm.view.project.edit
{
	import com.evola.dbsm.communication.Commands;
	import com.evola.dbsm.model.DatabaseBean;
	import com.evola.dbsm.model.ProjectBean;
	import com.evola.dbsm.model.UserBean;
	import com.evola.dbsm.service.RemoteServiceFacade;
	import com.evola.dbsm.service.ServiceResult;
	import com.evola.dbsm.view.navigation.NavigationPresenter;

	import flash.events.Event;
	import flash.events.MouseEvent;

	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.collections.ListCollectionView;
	import mx.controls.Alert;
	import mx.utils.ObjectUtil;
	import mx.validators.Validator;

	[Bindable]
	public class ProjectEditPresenter
	{

		public var view:ProjectEditView;
		public var model:ProjectEditModel=new ProjectEditModel();

		public function ProjectEditPresenter()
		{
		}

		public function selectView(index:int):void
		{

			if (index == 0)
			{

				RemoteServiceFacade.executeCommand(Commands.GET_OBJECT_COMMAND, model.project, function(result:ServiceResult):void
				{

					model.project=result.data as ProjectBean;
					model.selectedIndex=0;
				});
			}

			if (index == 1)
			{

				RemoteServiceFacade.executeCommand(Commands.LOAD_PROJECT_DATABASES_COMMAND, model.project.id, function(result:ServiceResult):void
				{

					model.databases=result.data as IList;
					model.selectedIndex=1;
				});
			}

			if (index == 2)
			{

				RemoteServiceFacade.executeCommand(Commands.LOAD_PROJECT_MEMBERS_COMMAND, model.project.id, function(result:ServiceResult):void
				{

					model.members=result.data as IList;
					model.selectedIndex=2;
				});
			}

		}

		public function addProject():void
		{

			model.prepareForInsertion();
		}

		public function editProject(project:ProjectBean):void
		{

			model.isInsert=false;

			RemoteServiceFacade.executeCommand(Commands.GET_OBJECT_COMMAND, project, function(result:ServiceResult):void
			{

				model.project=result.data as ProjectBean;
			});
		}

		public function saveBasicInfo():void
		{

			model.validateBasicInfo();

			if (!model.basicInfoError)
			{

				RemoteServiceFacade.executeCommand(Commands.EDIT_OBJECT_COMMAND, model.project, onProjectEdited);
			}
		}

		public function saveDatabase():void
		{

			model.validateDatabase();

			if (!model.databaseError)
			{

				if (model.currentDatabase.isPeristed)
				{
					RemoteServiceFacade.executeCommand(Commands.EDIT_OBJECT_COMMAND, model.currentDatabase, function(result:ServiceResult):void
					{

						model.editDatabase(result.data as DatabaseBean);

						model.currentDatabase=null;
					});
				}
				else
				{

					model.currentDatabase.project=model.project;

					RemoteServiceFacade.executeCommand(Commands.ADD_OBJECT_COMMAND, model.currentDatabase, function(result:ServiceResult):void
					{

						model.addDatabase(result.data as DatabaseBean);

						model.currentDatabase=null;
					});
				}
			}

		}

		public function save():void
		{

			if (model.isInsert)
				RemoteServiceFacade.executeCommand(Commands.ADD_OBJECT_COMMAND, model.project, onProjectAdded);
			else
				RemoteServiceFacade.executeCommand(Commands.EDIT_OBJECT_COMMAND, model.project, onProjectEdited);
		}

		private function onProjectAdded(result:ServiceResult):void
		{

			var project:ProjectBean=result.data as ProjectBean;

		}

		private function onProjectEdited(result:ServiceResult):void
		{


		}

		public function cancel():void
		{


		}

		public function addDatabase():void
		{

			var db:DatabaseBean=new DatabaseBean();

			model.currentDatabase=db;
		}

		public function editDatabase(database:DatabaseBean):void
		{

			model.currentDatabase=ObjectUtil.clone(database) as DatabaseBean;
		}

		public function deleteDatabase(database:DatabaseBean):void
		{

			RemoteServiceFacade.executeCommand(Commands.DELETE_OBJECT_COMMAND, database, function(result:ServiceResult):void
			{

				model.removeDatabase(result.data as DatabaseBean);

				model.currentDatabase=null;
			});
		}

		public function addNewMember():void
		{

			model.isAddingExistingUser=false;

			var db:UserBean=new UserBean();

			model.currentMember=db;
		}

		public function addExistingMember():void
		{

			model.isAddingExistingUser=true;

			RemoteServiceFacade.executeCommand(Commands.FIND_ALL_OBJECTS_COMMAND, new UserBean(), function(result:ServiceResult):void
			{

				model.allUsers=result.data as IList;
			});
		}

		public function editMember(user:UserBean):void
		{

			model.isAddingExistingUser=false;

			model.currentMember=ObjectUtil.clone(user) as UserBean;
		}

		public function deleteUser(user:UserBean):void
		{

			model.isAddingExistingUser=false;

			RemoteServiceFacade.executeCommand(Commands.DELETE_OBJECT_COMMAND, user, function(result:ServiceResult):void
			{

				model.removeMember(result.data as UserBean);

				model.currentMember=null;
			});
		}

		public function saveMember():void
		{

			model.validateMember();

			if (!model.membersError)
			{

				if (model.currentMember.isPeristed)
				{
					RemoteServiceFacade.executeCommand(Commands.EDIT_OBJECT_COMMAND, model.currentMember, function(result:ServiceResult):void
					{

						model.editMember(result.data as UserBean);

						model.currentMember=null;
					});
				}
				else
				{

					model.currentMember.projects=new ArrayCollection();
					model.currentMember.projects.addItem(model.project);

					RemoteServiceFacade.executeCommand(Commands.ADD_OBJECT_COMMAND, model.currentMember, function(result:ServiceResult):void
					{

						model.addMember(result.data as UserBean);

						model.currentMember=null;
					});
				}
			}

		}

		public function saveExistingMember():void
		{

			model.validateMember();

			if (!model.membersError)
			{

				var user:UserBean=model.allUsers.getItemAt(model.selectedUserIndex) as UserBean;

				if (!user.projects)
					user.projects=new ArrayCollection();

				user.projects.addItem(model.project);

				RemoteServiceFacade.executeCommand(Commands.EDIT_OBJECT_COMMAND, user, function(result:ServiceResult):void
				{

					model.addMember(result.data as UserBean);

					model.currentMember=null;
					model.isAddingExistingUser=false;
				});
			}

		}


	}
}
